<?php
/**
*
* 功能说明：文章控制器。
*
**/

namespace Qwadmin\Controller;
use Think\Controller;

class PicapiController extends Controller {
	
	public function _initialize(){
		C(setting());
	}
	public function index(){
		$m = M('photo');
		$p = I('page')?I('page'):0;
		$pagesize = I('page_size'); 
		$offset = $p*$pagesize; 
		$where['display'] = 0;
		$list = $m->order('id DESC')->limit($offset.','.$pagesize)->select();
		$num = $m->where($where)->count();
		if($num != 0){
			if($list){
			   foreach($list as $key=>$value){
					 $list[$key]['datetime'] = date("m-d H:i", $value['datetime']);
			   }
			   echo json_encode($list);
			}else{
				echo '0';
			}
		}else{
			echo '0';
		}

	}

	public function view(){
		$m = M('photo');
		$map['id'] = I('id');
		$id = I('id');
		$list = $m->where($map)->select();
		$qid = I('qid');
		$olist = $m->order('id DESC')->limit(0,1)->find(); 
		$wlist = $m->order('id ASC')->limit(0,1)->find(); 
		$where['vid'] = I('id');
		$countX = $m->where($map)->count();
		$numList = $m->count();
		if($countX != 0 && $list){
			$gzlists = M('photo_guanzhu')->where($where)->order('time DESC')->limit(88)->select();
			if($list){
			   foreach($list as $key=>$value){
					 $list[$key]['content'] = str_replaces($value['content']); 
			   }
			}
			$lists = array_merge_recursive($list[0],array("gzlist"=>$gzlists,"wlist"=>$wlist[id],"olist"=>$olist[id]));
			echo json_encode($lists);
		}else{
			if($numList != 0){
				$lists = array_merge_recursive($wlist,$olist);
				echo json_encode($lists[id]);
			}
		}
		
		
	}

	public function getGuanzhu(){
		# code...
		$cid = I('cid');
		$where['vid'] = I('vid');
		$openid = I('openid');
		$avatar = I('avatar');
		if($cid == 1){
			if($openid != null){
				$where['openid'] = I('openid');
				$num = M('photo_guanzhu')->where($where)->count();
				if($num > 0){
					$data['time'] = time();
					$gList = M('photo_guanzhu')->where($where)->save($data);
					if($gList){
						echo "ok-save";
					}else{
						echo "no-save";
					}
				}else{
					$data['vid'] = I('vid');
					$data['openid'] = I('openid');
					if($avatar != null && $avatar != 'undefined' ){
						$data['avatar'] = I('avatar');
					}else {
						$data['avatar'] = "https://api.55625.com/Public/Home/images/nouser.jpg";
					}
					$uname = I('uname');
					if($uname == 'undefined' && $uname != null){
						$data['uname'] = I('uname');
					}else{
						$data['uname'] = "游客";
					}
					$data['time'] = time();
					$gList = M('photo_guanzhu')->data($data)->add();
					if($gList){
						echo "ok-add";
					}else{
						echo "no-add";
					}
				}
			}else{
				echo "no openid";
			}
		}elseif ($cid = 2) {
			$gzList = M('photo_guanzhu')->where($where)->order('time DESC')->limit(88)->select();
			if($gzList){
				echo json_encode($gzList);
			}else{
				echo "no-data";
			}

		}




	}

	public function GetOpenid(){
		if (!isset($_GET['code'])){
			$baseUrl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$url = $this->__CreateOauthUrlForCode($baseUrl);
			Header("Location: $url");
			exit();
		} else {
		    $code = $_GET['code'];
			$openid = $this->getOpenidFromMp($code);
			echo $openid;
		}
	}
	private function __CreateOauthUrlForCode($redirectUrl){
		$urlObj["appid"] = C('appID');
		$urlObj["redirect_uri"] = "$redirectUrl";
		$urlObj["response_type"] = "code";
		$urlObj["scope"] = "snsapi_base";
		$urlObj["state"] = "STATE"."#wechat_redirect";
		$bizString = $this->ToUrlParams($urlObj);
		return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
	}
	private function ToUrlParams($urlObj){
		$buff = "";
		foreach ($urlObj as $k => $v){
			if($k != "sign"){
				$buff .= $k . "=" . $v . "&";
			}
		}
		$buff = trim($buff, "&");
		return $buff;
	}
	public function GetOpenidFromMp($code){
		$url = $this->__CreateOauthUrlForOpenid($code);
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$res = curl_exec($ch);
		curl_close($ch);
		$data = json_decode($res,true);
		$this->data = $data;
		$openid = $data['openid'];
		return $openid;
	}
	private function __CreateOauthUrlForOpenid($code){
		$urlObj["appid"] = C('appID');
		$urlObj["secret"] = C('appSecret');
		$urlObj["code"] = $code;
		$urlObj["grant_type"] = "authorization_code";
		$bizString = $this->ToUrlParams($urlObj);
		return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
	}

}
